<?php

namespace App\Http\Controllers\Backend\Shop;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Models\ShopType;
use App\Repository\Repositories\Interfaces\ShopRepository;
use Illuminate\Http\Request;

/**
 * 后台店铺模块
 *
 * @package App\Http\Controllers\Backend\Shop
 */
class ShopController extends BaseController
{

    protected $request;
    protected $shop_repo;

    public function __construct(ShopRepository $shop_repo,Request $request)
    {
        $this->request = $request;
        $this->shop_repo = $shop_repo;
    }

    public function index()
    {
        $list = $this->shop_repo->paginate($this->perPage);

        /*foreach($list['data'] as $k=> $v){
            $list['data'][$k]['sp_type_id_arr']=explode(",",$v['sp_type_id']);
            foreach($list['data'][$k]['sp_type_id_arr'] as $key=>$val){
                $list['data'][$k]['sp_type_id_type'][]=ShopType::query()->find($val);
            }
        }*/

        return $this->success(ErrorConst::SUCCESS_CODE, $list, true);
    }

    /**
     * 修改店铺状态
     * @param  int                                    $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        try {
            $status = $this->request->all(['sp_status','description']);
            $action = $this->shop_repo->update(['sp_status'=>$status['sp_status'],'description'=>$status['description']],$id);
            if ($action) {
                return $this->success("编辑操作成功",$action['data']);
            }
        }
        catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 推荐
     */
    public function recommend($id){
        $data=$this->request->all(['is_recommend']);
        $ret=$this->shop_repo->update($data,$id);
        return $this->success(ErrorConst::SUCCESS_CODE, $ret);
    }

}